unit JATUIF_Component_Checkbox;

{$mode objfpc}{$H+}

interface

uses
    Classes, SysUtils, Video, JATUIF_Component, JATUIF_Driver_Screen;

Type

    { TCheckbox }

    TColors = record
      Text_Color:Byte;
      Background_Color:Byte;
    end;

    TOnChange = procedure of object;

    TCheckbox = class(TComponent)
      Protected
        Procedure _OnKeyPress(Key:Char); override;
        Procedure _OnMouseEnter; override;
        Procedure _OnMouseLeave; override;
        Procedure _OnClick; override;
        Procedure _OnLoseFocus; override;
        Procedure Draw; override;
      Private
        { Private Properties & Methods }
        CheckX,CheckY:Integer;
        CheckValue:Boolean;
        CurrentColors:TColors;
        HighlightColors:TColors;
        StandardColors:TColors;
        BoxColors:TColors;
        function GetChecked:Boolean;
        procedure SetChecked(CheckBool:Boolean);
        procedure _OnChange;
      Public
        OnChange:TOnChange;
        Constructor Create(Parent:TScreen; X1,Y1:Integer);
        procedure SetBoxColors(Text,Background:Byte);
        procedure SetHighlighColors(Text,Background:Byte);
        procedure SetStandardColors(Text,Background:Byte);
        property Checked : Boolean read GetChecked write SetChecked;
    end;

implementation

{ TCheckbox }

Constructor TCheckbox.Create(Parent: TScreen; X1, Y1: Integer);
begin
     CheckValue:=False;
     CheckX:=X1;
     CheckY:=Y1;
     BoxColors.Background_Color:=Black;
     BoxColors.Text_Color:=White;
     HighlightColors.Background_Color:=White;
     HighlightColors.Text_Color:=Black;
     StandardColors.Background_Color:=Black;
     StandardColors.Text_Color:=White;
     CurrentColors:= StandardColors;
     OnChange:= @_OnChange;
     inherited Create(Parent, X1, Y1, X1+2, Y1);
end;

procedure TCheckbox.SetBoxColors(Text, Background: Byte);
begin
     BoxColors.Text_Color:= Text;
     BoxColors.Background_Color:= Background;
     Draw;
end;

procedure TCheckbox.SetHighlighColors(Text, Background: Byte);
begin
     HighlightColors.Text_Color:= Text;
     HighlightColors.Background_Color:= Background;
     Draw;
end;

procedure TCheckbox.SetStandardColors(Text, Background: Byte);
begin
     StandardColors.Text_Color:= Text;
     StandardColors.Background_Color:= Background;
     Draw;
end;

procedure TCheckbox.Draw;
begin
     { Draw Component }
     If GetChecked then begin
        Screen.DrawCharXY(GetTag,CheckX+0,CheckY,'[',BoxColors.Text_Color,BoxColors.Background_Color);
        Screen.DrawCharXY(GetTag,CheckX+1,CheckY,'x',CurrentColors.Text_Color,CurrentColors.Background_Color);
        Screen.DrawCharXY(GetTag,CheckX+2,CheckY,']',BoxColors.Text_Color,BoxColors.Background_Color);
     end else begin
         Screen.DrawCharXY(GetTag,CheckX+0,CheckY,'[',BoxColors.Text_Color,BoxColors.Background_Color);
         Screen.DrawCharXY(GetTag,CheckX+1,CheckY,' ',CurrentColors.Text_Color,CurrentColors.Background_Color);
         Screen.DrawCharXY(GetTag,CheckX+2,CheckY,']',BoxColors.Text_Color,BoxColors.Background_Color);
     end;
end;

function TCheckbox.GetChecked: Boolean;
begin
     Result:=CheckValue;
end;

procedure TCheckbox.SetChecked(CheckBool: Boolean);
begin
     CheckValue:= CheckBool;
     Draw;
     OnChange;
end;

procedure TCheckbox._OnChange;
begin
     { Do Nothing }
end;

{ Callback Events }

procedure TCheckbox._OnKeyPress(Key: Char);
begin
     inherited _OnKeyPress(Key);
end;

procedure TCheckbox._OnMouseEnter;
begin
     CurrentColors:= HighlightColors;
     Draw;
     inherited _OnMouseEnter;
end;

procedure TCheckbox._OnMouseLeave;
begin
     CurrentColors:= StandardColors;
     Draw;
     inherited _OnMouseLeave;
end;

procedure TCheckbox._OnClick;
begin
     Checked:= not Checked;
     inherited _OnClick;
end;

procedure TCheckbox._OnLoseFocus;
begin
     inherited _OnLoseFocus;
end;

end.

